/*
 * @file     g_bpu.h
 * @brief    This file provides all the BPU firmware functions.
 *
 * Change Logs:
 * Date           Author             Version        Notes
 * 2021-05-26     xubo               V1.0.0         the first version
 */

#ifndef __G_BPU_H__
#define __G_BPU_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "g_1903.h"
#include "g_system.h"
#include "g_vbat_ctr.h"
#include "g_otp.h"

/** @defgroup BPK Unit Block
  * @{
  */
#define BPK_KEY_REGION_0                      ((uint16_t)0x0001)
#define BPK_KEY_REGION_1                      ((uint16_t)0x0002)
#define BPK_KEY_REGION_2                      ((uint16_t)0x0004)
#define BPK_KEY_REGION_3                      ((uint16_t)0x0008)
#define BPK_KEY_REGION_ALL                    ((uint16_t)0x000f)
#define IS_BPK_KEY_REGION(REGION)             ((((REGION) & ~BPK_KEY_REGION_ALL) == 0x00) && ((REGION) != 0x00))
#define IS_BPK_LOCK(LOCK)                     ((((LOCK) & ~BPK_LR_LOCK_ALL) == 0x00))
#define BPK_KEY_NUM 32
/** @defgroup ALERT_Exported_Typedef
  * @{
  */
typedef enum
{
    Output_Z = 0,
    Output_Auto,
} ALERT_Port_Mode;

/** @defgroup Sensor Unit Block
  * @{
  */
#define EXT_SENSOR_NUM      8
#define INNER_SENSOR_NUM    5
#define IN_SHIELD_NUM       8

typedef struct
{
    uint32_t SENSOR_Port_Static;
    uint32_t SENSOR_Port_Dynamic;
    uint32_t SENSOR_Port_IE;
    uint32_t SENSOR_Port_PU;
    uint32_t SENSOR_Port_PD;
    uint32_t SENSOR_Static_Period;
    uint32_t SENSOR_Dynamic_Period;
    uint32_t SENSOR_Static_Chk_Num;
    uint32_t SENSOR_Dynamic_Chk_Num;
    uint32_t SENSOR_Static_Fit_Win;
    G_FunctionalState SENSOR_Static_Alert_Port_En;
    G_FunctionalState SENSOR_Dynamic_Alert_Port_En;
    ALERT_Port_Mode SENSOR_Static_Alert_Port_Mode;
    ALERT_Port_Mode SENSOR_Dynamic_Alert_Port_Mode;
    G_FunctionalState SENSOR_Static_Pull_Auto;
    uint32_t SENSOR_Static_Alert_H;
    uint32_t SENSOR_Static_Alert_L;
    uint32_t SENSOR_Static_Chk_Gap;
    uint32_t SENSOR_Static_Chk_Dly;
} G_SENSOR_EXTInitTypeDef;

/* 外部动\静态传感器管脚 */
#define SENSOR_Port_S0              BIT0
#define SENSOR_Port_S1              BIT1
#define SENSOR_Port_S2              BIT2
#define SENSOR_Port_S3              BIT3
#define SENSOR_Port_S4              BIT4
#define SENSOR_Port_S5              BIT5
#define SENSOR_Port_S6              BIT6
#define SENSOR_Port_S7              BIT7
#define SENSOR_Port_S01             (SENSOR_Port_S0 | SENSOR_Port_S1)
#define SENSOR_Port_S23             (SENSOR_Port_S2 | SENSOR_Port_S3)
#define SENSOR_Port_S45             (SENSOR_Port_S4 | SENSOR_Port_S5)
#define SENSOR_Port_S67             (SENSOR_Port_S6 | SENSOR_Port_S7)
#define SENSOR_Port_All             (SENSOR_Port_S01 | SENSOR_Port_S23 | SENSOR_Port_S45 | SENSOR_Port_S67)
#define IS_SENSOR_PORT(port)        ((((port) & (uint32_t)0xFFFFFF00) == 0x00) && ((port) != (uint32_t)0x00))

#define IS_SENSOR_PORT_IE(port)     (((port) & (uint32_t)0xFFFFFF00) == 0x00)

#define IS_SENSOR_PORT_PUPD(port)   (((port) & (uint32_t)0xFFFFFF00) == 0x00)

/* 外部动\静态传感器采样窗口 */
#define PERIOD_32MS                 ((uint32_t)0)
#define PERIOD_125MS                ((uint32_t)1)
#define PERIOD_500MS                ((uint32_t)2)
#define PERIOD_999MS                ((uint32_t)3)
#define IS_PERIOD_STATIC(period)    ((period == PERIOD_32MS) | \
                                    (period == PERIOD_125MS) | \
                                    (period == PERIOD_500MS) | \
                                    (period == PERIOD_999MS))

#define IS_PERIOD_DYNAMIC(period)   ((period == PERIOD_32MS) | \
                                    (period == PERIOD_125MS) | \
                                    (period == PERIOD_500MS) | \
                                    (period == PERIOD_999MS))

/* 外部静态传感器采样次数 */
#define SENSOR_STATIC_CHECK_Default         ((uint32_t)0x1)
#define SENSOR_STATIC_CHECK_1               ((uint32_t)0x0)
#define SENSOR_STATIC_CHECK_2               ((uint32_t)0x1)
#define SENSOR_STATIC_CHECK_3               ((uint32_t)0x2)
#define SENSOR_STATIC_CHECK_4               ((uint32_t)0x3)
#define IS_SENSOR_EXT_STATIC_CHECK(count)   (((count) == SENSOR_STATIC_CHECK_Default) |\
                                            ((count) == SENSOR_STATIC_CHECK_1) |\
                                            ((count) == SENSOR_STATIC_CHECK_2) |\
                                            ((count) == SENSOR_STATIC_CHECK_3) |\
                                            ((count) == SENSOR_STATIC_CHECK_4))

/* 外部动态传感器采样次数 */
#define SENSOR_DYNAMIC_CHECK_Default        ((uint32_t)0x2)
#define SENSOR_DYNAMIC_CHECK_1              ((uint32_t)0x0)
#define SENSOR_DYNAMIC_CHECK_2              ((uint32_t)0x1)
#define SENSOR_DYNAMIC_CHECK_3              ((uint32_t)0x2)
#define SENSOR_DYNAMIC_CHECK_4              ((uint32_t)0x3)
#define IS_SENSOR_EXT_DYNAMIC_CHECK(count)  (((count) == SENSOR_DYNAMIC_CHECK_Default) |\
                                            ((count) == SENSOR_DYNAMIC_CHECK_1) |\
                                            ((count) == SENSOR_DYNAMIC_CHECK_2) |\
                                            ((count) == SENSOR_DYNAMIC_CHECK_3) |\
                                            ((count) == SENSOR_DYNAMIC_CHECK_4))

#define SENSOR_STATIC_FILT_WINDOW_1         ((uint32_t)0x0)
#define SENSOR_STATIC_FILT_WINDOW_2         ((uint32_t)0x1)
#define SENSOR_STATIC_FILT_WINDOW_3         ((uint32_t)0x2)
#define SENSOR_STATIC_FILT_WINDOW_4         ((uint32_t)0x3)
#define IS_SENSOR_STATIC_FILT_WINDOW(COUNT) (((COUNT) == SENSOR_STATIC_FILT_WINDOW_1) |\
                                            ((COUNT) == SENSOR_STATIC_FILT_WINDOW_2) |\
                                            ((COUNT) == SENSOR_STATIC_FILT_WINDOW_3) |\
                                            ((COUNT) == SENSOR_STATIC_FILT_WINDOW_4))

#define IS_ALERT_Port_Mode(mode)    (((mode) == (ALERT_Port_Mode)Output_Z) | ((mode) == (ALERT_Port_Mode)Output_Auto))

#define IS_SENSOR_ALERT_LEVEL(port) (((port) & (uint32_t)0xFFFFFF00) == 0x00)

#define SENSOR_STATIC_CHK_GAP_1ms           ((uint32_t)0x0)
#define SENSOR_STATIC_CHK_GAP_2ms           ((uint32_t)0x1)
#define SENSOR_STATIC_CHK_GAP_4ms           ((uint32_t)0x2)
#define SENSOR_STATIC_CHK_GAP_8ms           ((uint32_t)0x3)
#define IS_SENSOR_EXT_STATIC_CHK_GAP(time)  (((time) == SENSOR_STATIC_CHK_GAP_1ms) |\
                                            ((time) == SENSOR_STATIC_CHK_GAP_2ms) |\
                                            ((time) == SENSOR_STATIC_CHK_GAP_4ms) |\
                                            ((time) == SENSOR_STATIC_CHK_GAP_8ms))

#define SENSOR_STATIC_CHK_DLY_1ms           ((uint32_t)0x0)
#define SENSOR_STATIC_CHK_DLY_4ms           ((uint32_t)0x1)
#define SENSOR_STATIC_CHK_DLY_8ms           ((uint32_t)0x2)
#define SENSOR_STATIC_CHK_DLY_16ms          ((uint32_t)0x3)
#define IS_SENSOR_EXT_STATIC_CHK_DLY(time)  (((time) == SENSOR_STATIC_CHK_DLY_1ms) |\
                                            ((time) == SENSOR_STATIC_CHK_DLY_4ms) |\
                                            ((time) == SENSOR_STATIC_CHK_DLY_8ms) |\
                                            ((time) == SENSOR_STATIC_CHK_DLY_16ms))

#define SENSOR_ANA_DUR_ALWAYS_ON    (uint32_t)0
#define SENSOR_ANA_DUR_2MS          (uint32_t)1
#define SENSOR_ANA_DUR_8MS          (uint32_t)2
#define SENSOR_ANA_DUR_16MS         (uint32_t)3
#define IS_SENSOR_ANA_DUR(dur)      (((dur) == SENSOR_ANA_DUR_ALWAYS_ON) |\
                                    ((dur) == SENSOR_ANA_DUR_2MS) |\
                                    ((dur) == SENSOR_ANA_DUR_8MS) |\
                                    ((dur) == SENSOR_ANA_DUR_16MS))

#define SENSOR_ANA_DEALY_31_25US    (uint32_t)0
#define SENSOR_ANA_DEALY_250US      (uint32_t)1
#define SENSOR_ANA_DEALY_1MS        (uint32_t)2
#define SENSOR_ANA_DEALY_4MS        (uint32_t)3
#define IS_SENSOR_ANA_DELAY(delay)  (((delay) == SENSOR_ANA_DEALY_31_25US) |\
                                    ((delay) == SENSOR_ANA_DEALY_250US) |\
                                    ((delay) == SENSOR_ANA_DEALY_1MS)   |\
                                    ((delay) == SENSOR_ANA_DEALY_4MS))

#define SENSOR_ANA_VBUT_1_2L            BIT0
#define SENSOR_ANA_VBUT_3_3H            BIT1
#define SENSOR_ANA_VBUT_3_3L            BIT2
#define SENSOR_ANA_TEMPERA_H            BIT3
#define SENSOR_ANA_TEMPERA_L            BIT4
#define SENSOR_ANA_ALL                  (BIT0 | BIT1 | BIT2 | BIT3 | BIT4)
#define IS_SENSOR_ANA(ana)              ((((ana) & (uint32_t)0xFFFFFFE0) == 0x00))

#define SENSOR_SHIELD_CHK_WIN_Default   ((uint32_t)0x2)
#define SENSOR_SHIELD_CHK_WIN_1         ((uint32_t)0x0)
#define SENSOR_SHIELD_CHK_WIN_2         ((uint32_t)0x1)
#define SENSOR_SHIELD_CHK_WIN_3         ((uint32_t)0x2)
#define SENSOR_SHIELD_CHK_WIN_4         ((uint32_t)0x3)
#define IS_SENSOR_SHIELD_CHK_WIN(count) (((count) == SENSOR_SHIELD_CHK_WIN_Default) |\
                                        ((count) == SENSOR_SHIELD_CHK_WIN_1) |\
                                        ((count) == SENSOR_SHIELD_CHK_WIN_2) |\
                                        ((count) == SENSOR_SHIELD_CHK_WIN_3) |\
                                        ((count) == SENSOR_SHIELD_CHK_WIN_4))

#define IS_PERIOD_SHIELD(period)    ((period == PERIOD_32MS) | \
                                    (period == PERIOD_125MS) | \
                                    (period == PERIOD_500MS) | \
                                    (period == PERIOD_999MS))

#define SHIELD_Port_S0              BIT0
#define SHIELD_Port_S1              BIT1
#define SHIELD_Port_S2              BIT2
#define SHIELD_Port_S3              BIT3
#define SHIELD_Port_S4              BIT4
#define SHIELD_Port_S5              BIT5
#define SHIELD_Port_S6              BIT6
#define SHIELD_Port_S7              BIT7
#define SHIELD_Port_All             (BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6 | BIT7)
#define IS_SHIELD_PORT(PORT)        ((((PORT) & (uint32_t)0xFFFFFF00) == 0x00))

#define SENSOR_IT_PORT_TAMPER_0         BIT0
#define SENSOR_IT_PORT_TAMPER_1         BIT1
#define SENSOR_IT_PORT_TAMPER_2         BIT2
#define SENSOR_IT_PORT_TAMPER_3         BIT3
#define SENSOR_IT_PORT_TAMPER_4         BIT4
#define SENSOR_IT_PORT_TAMPER_5         BIT5
#define SENSOR_IT_PORT_TAMPER_6         BIT6
#define SENSOR_IT_PORT_TAMPER_7         BIT7
#define SENSOR_IT_ANA_VBAT_1_2L         BIT8
#define SENSOR_IT_ANA_VBAT_3_3H         BIT9
#define SENSOR_IT_ANA_VBAT_3_3L         BIT10
#define SENSOR_IT_ANA_TEMPERA_H         BIT11
#define SENSOR_IT_ANA_TEMPERA_L         BIT12
#define SENSOR_IT_SHIELD_ALARM          BIT16
#define SENSOR_IT_SOFT_ATTACK           BIT17
#define SENSOR_IT_KEY_CLEAR             BIT18
#define IS_SENSOR_IT(IT)                ((IT & 0xFFF8E000) != 0)

#define SEC_LOCK_LOCK               BIT0
#define ACTION_LOCK                 BIT1
#define TAMPER_LOCK                 BIT2
#define SENSOR_LOCK                 BIT3
#define SHIELD_LOCK                 BIT4
#define IS_SENSOR_LOCK(LOCK)        ((LOCK & 0x1f) != 0)

#define TAMPER_IRQ                  BIT0
#define SENSOR_IRQ                  BIT1
#define SHIELD_IRQ                  BIT2
#define SOFT_ATTACK_IRQ             BIT3
#define KEY_CLEAR_IRQ               BIT4
#define ALL_IRQ                     (BIT0 | BIT1 | BIT2 | BIT3 | BIT4)
#define IS_SENSOR_IRQ(irq)          ((irq & 0x1f) != 0)

#define IS_SECTHRE_VAL(val)         (((val) <= 0xf) & ((val) > 0))

#define SENSOR_T_S_PORT_CLEAR(x, y)     (x &= ~((uint32_t)0xf << (y * 4)))
#define SENSOR_T_S_PORT_ENABLE(x, y)    (x |= ((uint32_t)0xa << (y * 4)))
#define SENSOR_T_S_PORT_DISABLE(x, y)   (x |= ((uint32_t)0x5 << (y * 4)))

#define IS_AB_DVDDLPM_VTHSEL(vthsel)   ((vthsel >= 0x00) && (vthsel <=0x0f))
#define IS_AC_DVDDLPM_VTHSEL(vthsel)   ((  vthsel == 0x01 || \
                                        vthsel == 0x02 || \
                                        vthsel == 0x04))

G_ErrorStatus G_BPU_Init(void);
G_ErrorStatus G_BPU_Reset(void);
void G_BPU_ITCmd(G_FunctionalState NewState);

G_ErrorStatus G_BPK_WriteKey(uint32_t *buf, uint32_t key_len, uint32_t key_offset);
G_ErrorStatus G_BPK_ReadKey(uint32_t *buf, uint32_t key_len, uint32_t key_offset);
void G_BPK_KeyClear(uint16_t bpk_key_region);
void G_BPK_AlterClearKey(G_FunctionalState NewState);
void G_BPK_KeyWriteLock(uint16_t bpk_key_region, G_FunctionalState status);
void G_BPK_KeyReadLock(uint16_t bpk_key_region, G_FunctionalState status);
void G_BPK_Lock(uint32_t bpk_locks, G_FunctionalState status);
void G_BPK_LockSelf(void);
G_FlagStatus G_BPK_GetLockStatus(uint32_t bpk_locks);

#if 0
uint32_t G_SENSOR_EXTInit(G_SENSOR_EXTInitTypeDef *SENSOR_EXTInitStruct);
void G_SENSOR_EXTPortCmd(uint32_t SensorPort, G_FunctionalState NewState);
void G_SENSOR_ANAConfig(uint32_t dur, uint32_t delay);
void G_SENSOR_ANACmd(uint32_t SensorAna, G_FunctionalState NewState);
void G_SENSOR_ANADvddLpmConfig(uint8_t vthsel);
void G_SENSOR_ANAVbutConfig(uint8_t vthhsel, uint8_t vthlsel);
void G_SENSOR_ANATsConfig(uint8_t utc, uint8_t otc);
void G_SENSOR_ShieldConfig(uint32_t period, uint32_t chknum);
void G_SENSOR_ShieldPortCmd(uint32_t ShieldPort, G_FunctionalState NewState);
void G_SENSOR_SecLockConfig(uint32_t SecLock, G_FunctionalState NewState);

G_Boolean G_SENSOR_GetITStatus(uint32_t G_ITStatus);
uint32_t G_SENSOR_GetITStatusReg(void);
void G_SENSOR_IRQCmd(uint32_t sec_irq, G_FunctionalState Statue);
void G_SENSOR_ClearITPending(void);
void G_SENSOR_ClearITPendingBit(uint32_t sec_irq);

void G_SENSOR_SetSecThreshold(uint32_t SecThre_val);
uint32_t G_SENSOR_GetSecCnt(void);
void G_SENSOR_ClearSecCount(void);
void G_SENSOR_ChkConfig(uint32_t SensorAna, G_FunctionalState NewState);
void G_SENSOR_Config(uint32_t SensorAna, G_FunctionalState NewState);
#endif

#ifdef __cplusplus
}
#endif

#endif

/************************ (C) COPYRIGHT GKT *****END OF FILE****/
